layui.define(['jquery', 'website', 'form', 'table', 'formSelects', 'laydate', 'xmSelect', 'validate'], function (exports) {
        let $ = layui.jquery,
            website = layui.website,
            table = layui.table,
            formSelects = layui.formSelects,
            xmSelect = layui.xmSelect,
            laydate = layui.laydate,
            validate = layui.validate,
            form = layui.form
        ;
        exports('clientModules', {
                /**
                 * 取消验证
                 * @param label 数组 标签去除前面的 * 号 [$("#levelTypeCodeLabel"),......]
                 * @param box 数组 输入框等取消验证 [$("#id")[0],......]
                 * @author Carey
                 */
                removeVerify: function (label, box) {
                    $.each(label, function (i, item) {
                        item.removeClass('website-form-item-require')
                    })
                    $.each(box, function (i, item) {
                        item.setAttribute('lay-verify', '')
                    })
                },
                /**
                 * 打开新增人员弹出框
                 * @param obj 方法传入参数
                 * @param tableIns table 实例
                 * @author Carey
                 */
                openMember: function (obj, tableIns, updateArray) {
                    // data 有值表示为修改
                    let data = obj.data,
                        lock,
                        type = data ? '修改' : '新增';

                    website.modal.open(type + '成员', 'client/addClassApplyMember', {
                        btn: ['提交', '重置'],
                        area: [$(window).width() <= 750 ? '95%' : '50%', '70%'],
                        offset: '30px',
                        yes: function (index, layero) {
                            if (!lock) {
                                lock = true
                                $('#website-class-apply-member-add').find('#submit').trigger('click');
                                lock = false;
                            }
                        },
                        success: function (layero, index) {
                            let alertMessage = type + '成功'
                            if (data) {
                                alertMessage = type + '成功'
                                let body = layero.find('#class-apply-member-add-form');
                                body.find("input[name=memberId]").val(data.memberId);
                                body.find("input[name=declareId]").val(data.declareId);
                                body.find("input[name=memberType]").val(data.memberType);
                                body.find("input[name=name]").val(data.name);
                                body.find("select[name=sexTypeCode]").val(data.sexTypeCode);
                                body.find("input[name=birthday]").val(data.birthday);
                                body.find("select[name=titleTypeCode]").val(data.titleTypeCode);
                                body.find("input[name=professionCode]").val(data.professionCode);
                                body.find("input[name=mobile]").val(data.mobile);
                                body.find("select[name=positionTypeCode]").val(data.positionTypeCode);
                                body.find("input[name=field]").val(data.field);
                                body.find("select[name=degreeTypeCode]").val(data.degreeTypeCode);
                                body.find("input[name=companyAddress]").val(data.companyAddress);
                                body.find("input[name=identity]").val(data.identity);
                                body.find("input[name=email]").val(data.email);
                                body.find("select[name=identityTypeCode]").val(data.identityTypeCode);
                                form.render();
                            }
                            form.on('submit(class-apply-member-add-form-submit)', function () {
                                let form = $('#website-class-apply-member-add').find('#class-apply-member-add-form'),
                                    temp = form.serializeArray(),
                                    formData = website.serializeObject(temp);

                                let cacheData = table.cache["classApplyMemberTable"];
                                for (let i = 0; i < cacheData.length; i++) {
                                    let item = cacheData[i];
                                    if (item.identity === formData.identity && !data) {
                                        lock = false;
                                        website.alert.warn('当前身份证号码已被重复添加，请重新核对信息！');
                                        return false;
                                    }
                                }
                                if (data) {
                                    cacheData.splice(obj.tr.data('index'), 1)//根据索引删除当前行
                                    cacheData.push(formData)
                                    data.memberId ? updateArray.push(data.memberId) : ''
                                } else {
                                    cacheData.push(formData)
                                }
                                tableIns.reload({
                                    data: cacheData
                                });
                                layer.close(index);
                                website.alert.success(alertMessage);
                                lock = false;
                                return false;
                            });
                        }
                    });
                },
                openProject: function (obj, tableIns) {
                    // data 有值表示为修改
                    let data = obj.data;
                    let lock;

                    website.modal.open('新增项目', 'client/addClassApplyProjectInfo', {
                        btn: ['提交', '重置'],
                        area: [$(window).width() <= 750 ? '95%' : '50%', '60%'],
                        offset: '30px',
                        maxHeight: '60%',
                        yes: function (index, layero) {
                            if (!lock) {
                                lock = true
                                $('#website-class-apply-project-add').find('#submit').trigger('click');
                                lock = false;
                            }
                        },
                        success: function (layero, index) {
                            if (data) {
                                let body = layero.find('#class-apply-project-add-form');
                                body.find("input[name=projectId]").val(data.projectId);
                                body.find("input[name=projectSource]").val(data.projectSource);
                                body.find("input[name=type]").val(data.type);
                                body.find("input[name=topicName]").val(data.topicName);
                                body.find("input[name=projectTime]").val(data.projectTime);
                                body.find("select[name=isFinish]").val(data.isFinish.toString());
                                form.render();
                            }
                            form.on('submit(class-apply-project-add-form-submit)', function () {
                                let form = $('#website-class-apply-project-add').find('#class-apply-project-add-form'),
                                    temp = form.serializeArray(),
                                    formData = website.serializeObject(temp);
                                let cacheData = table.cache["classApplyProjectInfoTable"];
                                if (data) {
                                    cacheData.splice(obj.tr.data('index'), 1)//根据索引删除当前行
                                    cacheData.push(formData)
                                } else {
                                    cacheData.push(formData)
                                }
                                tableIns.reload({
                                    data: cacheData
                                });
                                layer.close(index)
                                website.alert.success('新增成功');
                                lock = false;
                                return false;
                            });
                        }
                    });
                },
                addAchievement: function (obj, tableIns, array) {
                    // data 有值表示为修改
                    let data = obj ? obj.data : null
                    let titile = data ? '编辑拟完成的研究成果' : '新增拟完成的研究成果';
                    let lock;
                    layer.open({
                        type: 1
                        , title: titile//不显示标题栏
                        , shade: 0.4
                        , area: [$(window).width() <= 750 ? '95%' : '50%', '70%']
                        , id: 'LAY_layuinoreal' //设定一个id，防止重复弹出
                        , btn: ['提交', '关闭']
                        , btnAlign: 'c'
                        , zIndex: 3000
                        , moveType: 1 //拖拽模式，0或者1
                        , content:
                            '  <form id="updateAchievementForm" class="layui-form" style="margin-top: 10px;">\n' +
                            '<div class="layui-row layui-form-item" >\n' +
                            '        <div class="layui-inline  layui-col-xs8 layui-col-sm8 layui-col-md8 ">\n' +
                            '            <label class="layui-form-label website-form-item-require">成果形式：</label>\n' +
                            '                                    <input type="text" name="draftId"\n' +
                            '                                           class="layui-input website-hide">\n' +
                            '            <div class="layui-input-block">\n' +
                            ' <div name="performance" id="performance" lay-verify="required"> \n' +
                            '</div>' +
                            '            </div>\n' +
                            '        </div>\n' +
                            '</div>' +
                            '<div class="layui-row layui-form-item">\n' +
                            '        <div class=" layui-inline layui-col-xs8 layui-col-sm8 layui-col-md8 ">\n' +
                            '            <label class="layui-form-label website-form-item-require">成果字数：</label>\n' +
                            '<div class="layui-input-inline">\n' +
                            '                                    <input type="text" name="number" autocomplete="off"\n' +
                            '                  lay-verify="required"            class="layui-input">\n' +
                            '                                </div>\n' +
                            '                                <div class="layui-form-mid layui-word-aux">万字</div>' +
                            '        </div>\n' +
                            '</div>' +
                            '<div class="layui-row layui-form-item">\n' +
                            '        <div class=" layui-inline layui-col-xs8 layui-col-sm8 layui-col-md8 ">\n' +
                            ' <label class="layui-form-label ">完成时间</label>\n' +
                            '                                <div class="layui-input-block">\n' +
                            '                                    <input type="text" id="completeTime"\n' +
                            '                                           name="completeTime" autocomplete="off"\n' +
                            '                                           class="layui-input">\n' +
                            '                                </div>' +
                            '        </div>\n' +
                            '</div>' +
                            '    </form>\n'
                        , yes: function (index, layero) {
                            if (!lock) {
                                lock = true
                                let alertMessage = '新增成功',
                                    formData = $('#updateAchievementForm').serializeJson(),
                                    cacheData = table.cache["classDraftAchievementTable"];
                                if (!formData.select || !formData.number) {
                                    website.alert.warn('必填项不能为空');
                                    lock = false;
                                    return false;
                                }
                                formData.performance = formData.select
                                if (data) {
                                    cacheData.splice(obj.tr.data('index'), 1)//根据索引删除当前行
                                    cacheData.push(formData)
                                    alertMessage = '修改成功'
                                } else {
                                    cacheData.push(formData)
                                }
                                tableIns.reload({
                                    data: cacheData
                                });
                                layer.closeAll();
                                website.alert.success(alertMessage);
                                lock = false
                            }
                        }, success: function (layero, index) {
                            // formSelects.render();
                            let tempRender = {
                                el: '#performance',
                                data: array
                            }
                            if (data) {
                                let body = layero.find('#updateAchievementForm');
                                tempRender.initValue = data.performance.split(',');
                                body.find("input[name=number]").val(data.number);
                                body.find("input[name=draftId]").val(data.draftId);
                                body.find("input[name=completeTime]").val(data.completeTime);
                                form.render();
                            }
                            xmSelect.render(tempRender)
                            laydate.render({
                                elem: '#completeTime', //指定元素
                                trigger: 'click',
                            });
                        }
                    });
                },
                checkSubmit: function (submitFunction, args) {
                    website.modal.confirm('申请提交', '提交后信息不能再次修改，是否确认提交？', function () {
                        submitFunction(args);
                    });
                },
                updateEmail: function (ctx) {
                    layer.open({
                        type: 1
                        , title: '修改邮箱' //不显示标题栏
                        , shade: 0.3
                        , area: $(window).width() <= 750 ? '80%' : '30%'
                        , id: 'LAY_layui_update_email' //设定一个id，防止重复弹出
                        , btn: ['提交', '关闭']
                        , btnAlign: 'c'
                        , zIndex: 3000
                        , moveType: 1 //拖拽模式，0或者1
                        , content: '<div class="layui-row" id="updateEmailForm">\n' +
                            '    <form class="layui-form">\n' +
                            '        <div class="layui-form-item layui-col-xs9 layui-col-sm9 layui-col-md9 ">\n' +
                            '            <label class="layui-form-label website-form-item-require">邮箱：</label>\n' +
                            '            <div class="layui-input-block">\n' +
                            '                <input type="text" id="updateEmailInput" autocomplete="off" class="layui-input">\n' +
                            '            </div>\n' +
                            '        </div>\n' +
                            '<div class="layui-form-item">\n' +
                            '                                    <div class="layui-row layui-col-xs9 layui-col-sm9 layui-col-md9">\n' +
                            '                                        <div class=" layui-col-xs8 layui-col-sm8 layui-col-md8">\n' +
                            '                                            <label class="layui-form-label website-form-item-require">验证码：</label>\n' +
                            '            <div class="layui-input-block">\n' +
                            '                                            <input type="number" maxlength="4" name="verifyCode" lay-verify="required"\n' +
                            '                                                   placeholder="请输入验证码" class="layui-input" autocomplete="off">\n' +
                            '            </div>\n' +
                            '                                        </div>\n' +
                            '                                        <div class="layui-col-xs4">\n' +
                            '                                            <img class="codeimg" id="registerCodeImg"/>\n' +
                            '                                        </div>\n' +
                            '                                    </div>\n' +
                            '                                </div>\n' +
                            '<div class="layui-form-item">\n' +
                            '                                    <div class="layui-row layui-col-xs9 layui-col-sm9 layui-col-md9">\n' +
                            '                                        <div class="layui-col-xs8 layui-col-sm8 layui-col-md8">\n' +
                            '                                            <label class="layui-form-label website-form-item-require">邮箱验证码：</label>\n' +
                            '            <div class="layui-input-block">\n' +
                            '                                            <input type="number" maxlength="6" id="smsCode" name="smsCode" lay-verify="required"\n' +
                            '                                                   placeholder="请输入邮箱验证码" class="layui-input" autocomplete="off">\n' +
                            '            </div>\n' +
                            '                                        </div>\n' +
                            '                                        <div class="layui-col-xs4">\n' +
                            '                                            <button class="layui-btn layui-btn-normal layui-btn-fluid"\n' +
                            '                                                    id="get-sms-code">点击获取验证码\n' +
                            '                                            </button>\n' +
                            '                                        </div>\n' +
                            '                                    </div>\n' +
                            '                                </div>\n' +
                            '    </form>\n' +
                            '</div>'
                        , yes: function (index, layero) {
                            let $updateEmailForm = $('#updateEmailForm'),
                                email = $updateEmailForm.find('#updateEmailInput').val(),
                                smsCode = $updateEmailForm.find('input[name="smsCode"]').val();
                            let validateInfo = validate.email(email)
                            if (!validateInfo && email && smsCode) {
                                website.post(ctx + 'user/updateEmail', {
                                    email: email,
                                    smsCode: Number(smsCode)
                                }, function () {
                                    layer.closeAll();
                                    website.alert.success('修改成功！');
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(validateInfo);
                            }

                        }, success: function () {
                            let $getSMSCode = $('#get-sms-code'),
                                $updateEmailForm = $('#updateEmailForm');
                            initCode()

                            function initCode() {
                                $updateEmailForm.find('#registerCodeImg').attr("src", ctx + "images/captcha?data=" + new Date().getTime());
                            }

                            $updateEmailForm.find('#registerCodeImg').on('click', function () {
                                initCode();
                            });
                            $getSMSCode.on('click', function () {
                                let verifyCode = $updateEmailForm.find('input[name="verifyCode"]').val(),
                                    email = $updateEmailForm.find('#updateEmailInput').val();
                                let validateInfo = validate.email(email)
                                if (!validateInfo && email) {
                                    $.get(ctx + 'register/getEmailCode/' + email, {verifyCode: verifyCode}, function (data) {
                                        if (data.code === 200) {
                                            if (data.data) {
                                                layer.msg(data.data);
                                            } else {
                                                layer.alert("邮件发送成功，请注意查收！验证码有效期 180秒！");
                                            }
                                        } else {
                                            layer.msg(data.message);
                                            if (data.code === 500) {
                                                initCode();
                                            }
                                        }
                                    })
                                } else {
                                    layer.msg(validateInfo);
                                }
                                return false;
                            });
                        }
                    });
                },
                updatePassword: function (ctx) {
                    layer.open({
                        type: 1
                        , title: '修改登录密码' //不显示标题栏
                        , shade: 0.3
                        , area: $(window).width() <= 750 ? '80%' : '30%'
                        , id: 'LAY_layui_update_password' //设定一个id，防止重复弹出
                        , btn: ['提交', '关闭']
                        , btnAlign: 'c'
                        , zIndex: 3000
                        , moveType: 1 //拖拽模式，0或者1
                        , content:
                            '    <form id="updatePasswordForm" class="layui-form">\n' +
                            '<div class="layui-row" >\n' +
                            '        <div class="layui-form-item layui-col-xs8 layui-col-sm8 layui-col-md8 ">\n' +
                            '            <label class="layui-form-label website-form-item-require">旧密码：</label>\n' +
                            '            <div class="layui-input-block">\n' +
                            '                <input type="password" name="oldPass" id="passwordInput" autocomplete="off" class="layui-input">\n' +
                            '            </div>\n' +
                            '        </div>\n' +
                            '</div>' +
                            '<div class="layui-row" id="updatePasswordForm">\n' +
                            '        <div class="layui-form-item layui-col-xs8 layui-col-sm8 layui-col-md8 ">\n' +
                            '            <label class="layui-form-label website-form-item-require">新密码：</label>\n' +
                            '            <div class="layui-input-block">\n' +
                            '                <input type="password" name="newPass" id="updatePasswordInput" autocomplete="off" class="layui-input">\n' +
                            '            </div>\n' +
                            '        </div>\n' +
                            '</div>' +
                            '<div class="layui-row">\n' +
                            '        <div class="layui-form-item layui-col-xs8 layui-col-sm8 layui-col-md8 ">\n' +
                            '            <label class="layui-form-label website-form-item-require">确认密码：</label>\n' +
                            '            <div class="layui-input-block">\n' +
                            '                <input type="password" name="newPass2" id="updatePasswordInputReview" autocomplete="off" class="layui-input">\n' +
                            '            </div>\n' +
                            '        </div>\n' +
                            '</div>' +
                            '    </form>\n'

                        , yes: function (index, layero) {
                            let param = $('#updatePasswordForm').serializeJson();
                            let password = param.newPass;
                            let passwordReview = param.newPass2;
                            if (!password || !passwordReview || !param.oldPass) {
                                website.alert.warn('请输入必填项！');
                                return false
                            }
                            if (password === passwordReview) {
                                website.post(ctx + 'user/updatePass', {
                                    newPass: password,
                                    newPass2: passwordReview,
                                    oldPass: param.oldPass
                                }, function () {
                                    layer.closeAll();
                                    website.alert.success('修改成功！');
                                });
                            } else {
                                website.alert.warn('新密码输入不一致，请确认输入密码！');
                                return false
                            }
                        }
                    });
                }
            }
        )
    }
)